/*
 * Created by JFormDesigner on Fri Nov 08 10:55:01 CST 2024
 */

package com.hafu.view.RuKu;

import java.awt.*;
import java.awt.event.*;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.*;
import javax.swing.table.*;

import com.hafu.model.GysInfo;
import com.hafu.model.RuKuDetail;
import com.hafu.model.RuKuMain;
import com.hafu.model.SpInfo;
import com.hafu.service.GysInfoService;
import com.hafu.service.RuKuService;
import com.hafu.service.SpInfoService;
import com.hafu.util.StringUtil;
import com.sun.xml.internal.ws.util.StringUtils;
import net.miginfocom.swing.*;

/**
 * @author aa
 */
public class AddRuKuFrm extends JFrame {
    private GysInfoService gysInfoService=new GysInfoService();
    private SpInfoService spInfoService=new SpInfoService();
    private TableColumn tableColumn;
    private RuKuService ruKuService = new RuKuService();

    public AddRuKuFrm() {
        initComponents();
        fillComboBox();
    }
public void fillComboBox(){
    ArrayList<GysInfo> gysInfoByName=gysInfoService.getGysInfoByName(new GysInfo());
    for (GysInfo gysInfo : gysInfoByName){
        comboBox1.addItem(gysInfo.getName());
    }
    JComboBox<SpInfo> comboBox = new JComboBox<>();
    ArrayList<SpInfo> spInfoByName=spInfoService.getSpInfoByName(new SpInfo());
    for (SpInfo spInfo : spInfoByName){
        comboBox.addItem(spInfo);
    }
    DefaultTableModel defaultTableModel=(DefaultTableModel) table1.getModel();
    tableColumn=table1.getColumnModel().getColumn(0);
    tableColumn.setCellEditor(new DefaultCellEditor(comboBox));
}
    private void button1(ActionEvent e) {
        // TODO add your code here
        DefaultTableModel defaultTableModel=(DefaultTableModel) table1.getModel();
        defaultTableModel.addRow(new Vector());
    }

    private void button2(ActionEvent e) {
        // TODO add your code here
        RuKuMain ruKuMain = new RuKuMain();
        ruKuMain.setRukuName(textField1.getText());
        ruKuMain.setGysName(comboBox1.getSelectedItem().toString());
        DefaultTableModel defaultTableModel=(DefaultTableModel) table1.getModel();
        int rowCount = defaultTableModel.getRowCount();
        ArrayList<RuKuDetail> rukudetailList=new ArrayList<>();
        BigDecimal totalPrice=new BigDecimal(0);
        for (int i = 0; i < rowCount; i++) {
            SpInfo SpInfo1=(SpInfo)table1.getValueAt(i,0);
            if (SpInfo1==null){
                continue;
            }
            if (StringUtil.isEmpty(SpInfo1.getName())){
                continue;
            }
            String s=table1.getValueAt(i,1).toString();
            Integer num=Integer.parseInt(s);
            RuKuDetail ruKuDetail = new RuKuDetail();
            ruKuDetail.setSpName(SpInfo1.getName());
            ruKuDetail.setNum(num);
            ruKuDetail.setPrice(SpInfo1.getPrice());
            rukudetailList.add(ruKuDetail);
            totalPrice=totalPrice.add(SpInfo1.getPrice().multiply(new BigDecimal(num)));
        }
        ruKuMain.setRuKudetaillist(rukudetailList);
        ruKuMain.setPrice(totalPrice);
        Boolean b = ruKuService.addRuKu(ruKuMain);
        if (b){
            JOptionPane.showMessageDialog(null,"添加成功");
            this.dispose();
        }else {
            JOptionPane.showMessageDialog(null,"添加失败");
        }
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        label1 = new JLabel();
        textField1 = new JTextField();
        label2 = new JLabel();
        comboBox1 = new JComboBox();
        button1 = new JButton();
        button2 = new JButton();
        scrollPane1 = new JScrollPane();
        table1 = new JTable();

        //======== this ========
        setTitle("\u6dfb\u52a0\u5165\u5e93\u5355");
        Container contentPane = getContentPane();
        contentPane.setLayout(new MigLayout(
            "hidemode 3",
            // columns
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]",
            // rows
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]"));

        //---- label1 ----
        label1.setText("\u5165\u5e93\u540d\u79f0");
        contentPane.add(label1, "cell 2 0 9 1");
        contentPane.add(textField1, "cell 2 0 9 1");

        //---- label2 ----
        label2.setText("\u4f9b\u5e94\u5546\u9009\u62e9");
        contentPane.add(label2, "cell 13 0 3 1");
        contentPane.add(comboBox1, "cell 16 0");

        //---- button1 ----
        button1.setText("\u6dfb\u52a0\u65b0\u884c");
        button1.addActionListener(e -> button1(e));
        contentPane.add(button1, "cell 19 0");

        //---- button2 ----
        button2.setText("\u63d0\u4ea4\u5165\u5e93");
        button2.addActionListener(e -> button2(e));
        contentPane.add(button2, "cell 22 0");

        //======== scrollPane1 ========
        {

            //---- table1 ----
            table1.setModel(new DefaultTableModel(
                new Object[][] {
                    {null, null},
                },
                new String[] {
                    "\u5546\u54c1", "\u6570\u91cf"
                }
            ));
            scrollPane1.setViewportView(table1);
        }
        contentPane.add(scrollPane1, "cell 3 4 21 1");
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JLabel label1;
    private JTextField textField1;
    private JLabel label2;
    private JComboBox comboBox1;
    private JButton button1;
    private JButton button2;
    private JScrollPane scrollPane1;
    private JTable table1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
